<?php
namespace Dashboard\Controller;

use Think\Controller;
use Think\Session\Driver\Memcache;

class BeautyController extends BaseController{
    //轮播图
    public function index(){
        $a = (int)($_POST["currpage"]);//跳转页数
        if($a != null){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        $totalpage = 0;//总页数
        $pagesize  = 10;//每页显示数
        //搜索
        $sou     = $_POST['sou'];
        $content = $_POST['keywords'];
        if($sou != null){ //标题
            $currpage  = 1;
            $totalpage = 1;//总页数
            if($sou == 1){
                $list      = sels("news_lun","title like '%{$content}%'");
            }
            if($sou == 2){
                $list      = sels("news_lun","jianjie like '%{$content}%'");
            }
            if($sou == 3){
                $list      = sels("news_lun","content like '%{$content}%'");
            }
        }else{
            $arr       = currpage("news_lun","","$pagesize","$currpage","id desc");
            $totalpage = $arr['totalpage'];
            $list      = $arr['list'];
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    public function form(){
        $id      = I('id');
        if($id != null){
            $list = sel("news_lun","",array("id"=>$id));
            $type = $this->gong_g($list['status']);
            $this->assign("list",$list)->assign("types",$type);
        }else{
            $type = $this->gong_g(1);
            $this->assign("types",$type);
        }
        $this->display();
    }
    public function add(){
        $data['status']      = I('status');
        $data['type']        = I('type');
        $id                   = I('id');
        //<!--1 商品 2资讯  3会员卡  4攻略课堂 5线上报名 6题库-->
        $img                 = I('img');
        //处理图片
        if($_FILES['one']['name'] != null){
            unlink($img);
            $info                   = $this->imgUpload("banner/");
            $savename               = $info['one']['savename'];
            $savepath               = $info['one']['savepath'];
            $data['img']            = "Data/".$savepath.$savename;
        }
        $data['lun_id']     = I('lun_id');
        if($id == null){
            $data['create_at'] = time();
            $result = ins("news_lun",$data);
            if($result){
                $logs = logs("添加了轮播图信息：{$data['type']}");
                $this->success("添加成功",U('form'));}
        }else{
            $data['up_at']     = time();
            $result = up_one("news_lun",array("id"=>$id),$data);
            if($result){
                $logs = logs("修改了轮播图信息：{$data['type']}");
                $this->success("修改成功",U('index'));}else{$this->success("没有信息变更，修改失败");}
        }
    }
    public function lun(){
        $status = I('status');
         //<!--1 商品 2资讯  3会员卡  4攻略课堂 5线上报名 6题库-->
        $list = $this->gong_g($status);
        $this->ajaxReturn(array("info"=>$list));
    }
    public function gong_g($status){
        if($status == 1){$list = sels("goods","id,name");}
        if($status == 2){$list = sels("news","id,title as name");}
        if($status == 3){
            $list = joinss("vips","a.id,a.tags,b.name",array("a left join __VIP_TYPE__ b on a.vip_type = b.id"));
            foreach ($list as $k=>$v){
                $list[$k]['name'] = $v['name']."(".$v['tags'].")";
            }
        }
        if($status == 4){$list = sels("schoolroom","id,title as name");}
        if($status == 5){$list = sels("line_top","id,name");}
        if($status == 6){$list = sels("questions","id,name");}
        if(!$list){$list = 1;}
        return $list;
    }
    //新闻
    public function news(){
        $a = (int)($_POST["currpage"]);//跳转页数
        if($a != null){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        $totalpage = 0;//总页数
        $pagesize  = 10;//每页显示数
        //搜索
        $sou     = $_POST['sou'];
        $content = $_POST['keywords'];
        if($sou != null){ //标题
            $currpage  = 1;
            $totalpage = 1;//总页数
            if($sou == 1){
                $list      = sels("news","lei = 1 and title like '%{$content}%'");
            }
            if($sou == 2){
                $list      = sels("news","lei = 1 and content like '%{$content}%'");
            }
        }else{
            $arr       = currpage("news","lei = 1","$pagesize","$currpage","id desc");
            $totalpage = $arr['totalpage'];
            $list      = $arr['list'];
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    public function news_vip(){
        $a = (int)($_POST["currpage"]);//跳转页数
        if($a != null){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        $totalpage = 0;//总页数
        $pagesize  = 10;//每页显示数
        //搜索
        $sou     = $_POST['sou'];
        $content = $_POST['keywords'];
        if($sou != null){ //标题
            $currpage  = 1;
            $totalpage = 1;//总页数
            if($sou == 1){
                $list      = sels("news","lei = 2 and title like '%{$content}%'");
            }
            if($sou == 2){
                $list      = sels("news","lei = 2 and content like '%{$content}%'");
            }
        }else{
            $arr       = currpage("news","lei = 2","$pagesize","$currpage","id desc");
            $totalpage = $arr['totalpage'];
            $list      = $arr['list'];
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    public function news_form(){
        $id      = I('id');
        if($id != null){
            $list = sel("news","",array("id"=>$id));
            $this->assign("list",$list);
        }
        $this->display();
    }
    public function news_add(){
        $id               = $_POST['id'];
        $data['title']   = I('title');
        $type   = I('type');
        if($type){
            $data['type']   = $type;
        }else{
            $data['type']   = 1;
        }
        $lei             = I('lei');
        if($lei == 2){
            $data['lei']         = $lei;
            $data['vip_moneys'] = I('vip_moneys');
            $data['phone']       = I('phone');
            $data['moneys']      = I('moneys');
        }
        $data['nums']   = I('nums');
        $data['content'] = I('content');
        if($id == null){
            $data['create_at'] = time();
            //处理图片
            if($_FILES['one']['name'] != null){
                $info                   = $this->imgUpload("banner/");
                $savename               = $info['one']['savename'];
                $savepath               = $info['one']['savepath'];
                $data['img']            = "Data/".$savepath.$savename;
            }
            $result = ins("news",$data);
            if($result){
                $logs = logs("添加了资讯信息：{$data['title']}");
                $this->success("添加成功",U('news_form'));}
        }else{
            $data['up_at']     = time();
            $img                = I('img');
            //处理图片
            if($_FILES['one']['name'] != null){
                unlink($img);
                $info                   = $this->imgUpload("banner/");
                $savename               = $info['one']['savename'];
                $savepath               = $info['one']['savepath'];
                $data['img']            = "Data/".$savepath.$savename;
            }
            $result = up_one("news",array("id"=>$id),$data);
            if($result){
                $logs = logs("修改了资讯信息：{$data['title']}");
                if($lei == 2){$this->success("修改成功",U('news_vip'));}else{$this->success("修改成功",U('news'));}
            }else{
                $this->success("没有信息变更，修改失败");
            }
        }
    }
    //购买用户信息
    public function user(){
        $a = (int)($_POST["currpage"]);//跳转页数
        if($a != null){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        $totalpage = 0;//总页数
        $pagesize  = 10;//每页显示数
        //搜索
        $sou     = $_POST['sou'];
        $content = $_POST['content'];
        //参数 1 商品  2 线上报名 3攻略课堂报名
        $where = "a.type = 5";
        $table = "orders";
        $filed = "a.id,a.trade_no,a.create_at,b.name,b.phone,b.avatar,a.moneys,c.title";
        $join  = array("a left join __USERS_JIINFO__ b on a.user_id = b.user_id","left join __NEWS__ c on a.goods_id = c.id");
        if($sou != null){ //标题
            $currpage  = 1;
            $totalpage = 1;//总页数
            if($sou == 1){
                $list      = joinss($table,$filed,$join,"{$where} and b.name like '%{$content}%'","a.id desc");
            }
            if($sou == 2){
                $list      = joinss($table,$filed,$join,"{$where} and b.phone like '%{$content}%'","a.id desc");
            }
            if($sou == 3){
                $list      = joinss($table,$filed,$join,"{$where} and c.title like '%{$content}%'","a.id desc");
            }
        }else{
            $arr       = currpages($table,$filed,$join,$where,"a.id desc",$pagesize,$currpage);
            $totalpage = $arr['totalpage'];
            $list      = $arr['list'];
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    //资讯分类列表
    public function type()
    {
        $pid = M("mei_type")->order("id")->select();
        $list = get_attr($pid,0);
        $this->assign("list", $list);
        $this->display();
    }
    //资讯分类--添加列表
    public function type_form()
    {
        $currpage = I('currpage');
        $this->assign("currpage", $currpage);
        $pid = M("mei_type")->where("id != 1 and id !=4")->order("pid")->select();
        $fen = get_attr($pid,0);
        $id = I('id');
        if ($id) {
            $list = sel("mei_type", " * ", "id = {$id}");
            $this->assign("list", $list);
        }
        $this->assign("pid", $fen);
        $this->display();
    }
    //资讯分类--数据添加
    public function upload(){
        $id = I('id');
        $name = I("name");
        $pid = I("pid");
        if ($id == 0) {
            $res = ins("mei_type", array("name" => $name, "create_at" => time(),"pid"=>$pid));
            if ($res) {
                $logs = logs("添加了美容分类：{$name}");
                $this->success("添加成功", U("type"));
            }
        } else {
            $res = up_one("mei_type", array("id" => $id), array("name" => $name, "pid"=>$pid,"up_at" => time()));
            if ($res) {
                $logs = logs("修改了美容分类:{$name}");
                $this->success("修改成功", U("type"));
            }
        }
    }
    //资讯信息
    public function info(){
        $list = sels("news","*","type_two_id < 1","id desc");
        if($list){
            foreach ($list as $k=>$v){
                $ke_name = sel("goods_zhu","id,title","id = {$v['ke_id']}");
                $list[$k]['ke_name'] = $ke_name['title'];
                $lei_name = sel("mei_type","id,name","id = {$v['lei_id']}");
                $list[$k]['lei_name'] = $lei_name['name'];
            }
        }
        $this->assign("list", $list);
        $this->display();
    }
    //资讯信息--添加
    public function forms()
    {
        $id = I('id');
        $type = sels("mei_type","id,name","pid=0 and id != 1 and id != 4");
        $info = sels("goods_zhu","id,title");
        $this->assign("type",$type)->assign("info",$info);
        if ($id) {
            $list = sel("news", " * ", array("id" => $id));
            $this->assign("list", $list);
        }
        $this->display();
    }
    //资讯信息--数据添加
    public function adds(){
        $currpage = I('currpage');
        $data['type_two_id'] = 0;//分类名称
        $data['lei_id'] = I('lei_id');//分类名称
        $data['ke_id'] = I('ke_id');//视频名称
        $data['phone'] = I('phone');
        $data['content'] = I('content');
        $id = I('id');
        //处理图片
        if ($_FILES['img']['name'] != null) {
            $img = I('img');unlink($img);
            $info = $this->imgUpload("info/");
            $savename = $info['img']['savename'];
            $savepath = $info['img']['savepath'];
            $data['img'] = "Data/" . $savepath . $savename;
        }
        if ($id == null) {
            $data['create_at'] = time();
            $result = ins("news", $data);
            if ($result) {
                $content = "管理员：" . $_SESSION['admin']['numbers'] . " 于 " . date("Y-m-d H:i:s", time()) . "添加了门店/院线推荐，名称为：‘" . $data['title'] . "’";
                $log = ins("logs", array("content" => $content, "admin_id" => $_SESSION['admin']['id'], "create_at" => time()));
                $this->success("添加成功", U('info'));
            }
        } else {
            $data['up_at'] = time();
            $result = up_one("news", array("id" => $id), $data);
            if ($result) {
                $content = "管理员：" . $_SESSION['admin']['numbers'] . " 于 " . date("Y-m-d H:i:s", time()) . "修改了门店/院线推荐信息，修改改后的名称为：‘" . $data['title'] . "’";
                $log = ins("logs", array("content" => $content, "admin_id" => $_SESSION['admin']['id'], "create_at" => time()));
                $this->success("修改成功", U('info'));
            } else {
                $this->success("没有信息变更，修改失败");
            }
        }
    }
}